/**
 * Description:读取XML文件，并解析
 * Company:济南东之林智能软件有限公司
 * @author lld
 * @version 1.0
 * */
package com.douyu.water.util;

import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class ParseXmlFile 
{
 /**
  * 解析XML文件
  * @param filePath需要解析的XML文件绝对路径
  * */
	public XmlConfig parseXml(String filePath)
	{
		if(filePath == null || filePath.trim().equals(""))
		{
			return null;
		}
		File xmlFile = new File(filePath.trim());
		if(!xmlFile.exists())
		{
			return null;
		}
		XmlConfig config = new XmlConfig();
		Document doc = null;
		try
		{
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setIgnoringElementContentWhitespace(true);
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse(xmlFile);
			//开始解析xml文件
			NodeList rootList = doc.getChildNodes();//获取根标签
			NodeList nodeList = null;
			int length = rootList.getLength();
			//获取配置标签
			for(int i = 0;i < length;i++)
			{
				Node temp = rootList.item(i);
				if(temp.getNodeName().trim().equalsIgnoreCase("setting"))
				{
					nodeList = temp.getChildNodes();
					break;
				}
			}
			
		    length = nodeList.getLength();
			for(int i = 0;i < length ;i++)
			{
				Node node = nodeList.item(i);
				String nodeName = node.getNodeName();
				//处理节点
				if(nodeName.trim().equalsIgnoreCase("colnum"))
				{
					String colNum = node.getTextContent();
					if(colNum == null || colNum.trim().equals(""))
					{
						return null;
					}
					try
					{
						config.setColumnNum(Integer.parseInt(colNum.trim()));
					}
					catch(Exception e)
					{
						return null;
					}
				}
				else if (nodeName.trim().equalsIgnoreCase("columns"))
				{
					//处理每一列的配置
					NodeList childNode = node.getChildNodes();
					int len = childNode.getLength();
					String[] colNames = new String[config.getColumnNum()];
					String[] fieldName = new String[config.getColumnNum()];
					int[] refindex = new int[config.getColumnNum()];
					String[] refcon = new  String[config.getColumnNum()];
					int[] widths = new int[config.getColumnNum()];
					String[] type =  new  String[config.getColumnNum()];
					for(int j = 0;j < len;j++)
					{
						Node col = childNode.item(j);
						String tempName = col.getNodeName();
						
						if(!tempName.trim().equals("col"))
						{
							continue;
						}
						NamedNodeMap attr = col.getAttributes();
						Node index = attr.getNamedItem("index");//列号
						Node property = attr.getNamedItem("property");//属性
						Node colName = attr.getNamedItem("colname");//列标题
						Node ref = attr.getNamedItem("refindex");//关联列
						Node con = attr.getNamedItem("refcon");//关联条件
						Node cellType = attr.getNamedItem("type");
						String _index = null;
						//封装数据到设置类中
						if(index != null)
						{
							_index = index.getNodeValue();
						}												
						if(_index == null || _index.trim().equals(""))
						{
							continue;
						}
						try
						{
							int arrindex = Integer.parseInt(_index.trim());
							String _property = null;
							if(property != null)
							{
								_property = property.getNodeValue();
								fieldName[arrindex -1] = _property;
							}
							String _colName = null;
							if(colName != null)
							{
								_colName = colName.getNodeValue();
								colNames[arrindex - 1] = _colName;
							}
							String _ref = null;
							if(ref != null)
							{
								_ref = ref.getNodeValue();
								try
								{
									refindex[arrindex - 1] = Integer.parseInt(_ref.trim());
								}
								catch(Exception e)
								{
									refindex[arrindex - 1] = 0;
								}
							}
							String _type = null;
							if(cellType != null)
							{
								_type = cellType.getNodeValue();
							}
							type[arrindex - 1] = _type;
							String _con = null;
							if(con != null)
							{
								_con = con.getNodeValue();
							}
							refcon[arrindex - 1] = _con;
						}
						catch(Exception e)
						{
							continue;
						}
					}
					config.setColumnName(colNames);
					config.setFieldName(fieldName);
					config.setRefcon(refcon);
					config.setRefindex(refindex);
					config.setCellType(type);
				} 
				
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return config;
	}
	public static void main(String[] args)
	{
		ParseXmlFile xml = new ParseXmlFile();
		String file = ParseXmlFile.class.getResource("/configs/report/record.xml").getFile();
		xml.parseXml(file);
	}
}
